﻿
using Furion;
using GWB.Core;
using GWB.Core.TableEntity.Common;
using Mapster;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace GWB.Supplier
{
    public class SqlSugarRepository<T> : SimpleClient<T> where T : BaseEntity, new()
    {

        public SqlSugarRepository(ISqlSugarClient context = null) : base(context)//默认值等于null不能少
        {
            base.Context = SqlSugarDb.Db;
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public virtual async Task<T> Create(T input)
        {
            var property = input.GetType().GetProperty("Id");
            if (property != null)
                property.SetValue(input, YitIdHelper.NextId());
            return await AsInsertable(input).ExecuteReturnEntityAsync();
        }

        public virtual async Task<bool> Create(List<T> input)
        {
            foreach (var property in input.Select(item => item.GetType().GetProperty("Id"))
                         .Where(property => property != null))
            {
                property.SetValue(input, YitIdHelper.NextId());
            }

            return await AsInsertable(input).ExecuteCommandIdentityIntoEntityAsync();
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public virtual async Task<bool> Update(T input)
        {
            return await SqlSugarDb.Db.Updateable(input)
                  .IgnoreColumns(it => new { it.CreateAt, it.CreateBy, it.IsDeleted })
                  .ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<bool> Delete(long id)
        {
            return await Delete(x => x.Id == id);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public virtual async Task<bool> Delete(List<long> ids)
        {

            return await Delete(x => ids.Contains(x.Id));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        public virtual async Task<bool> Delete(Expression<Func<T, bool>> whereExpression)
        {
            return await SqlSugarDb.Db.Updateable<T>()
                    .SetColumns(x => new T { IsDeleted = true })
                    .Where(whereExpression)
                   .ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public virtual async Task<PageResult<T>> Page(PageInput input)
        {

            var papge = new PageModel() { PageIndex = input.Current, PageSize = input.PageSize };
            List<IConditionalModel> conModels = new List<IConditionalModel>();

            foreach (System.Reflection.PropertyInfo p in input.GetType().GetProperties())
            {
                if (p.Name != "PageIndex" && p.Name != "PageSize")
                {
                    conModels.Add(new ConditionalModel()
                    {
                        FieldName =p.Name, 
                        ConditionalType = ConditionalType.Equal, 
                        FieldValue =p.GetValue(p.Name)?.ToString()
                    });;
                }
            }

            var data = await base.GetPageListAsync(conModels, papge);
            return new PageResult<T>()
            {
                Data = data.Adapt<List<T>>(),
                Total = papge.TotalCount
            };
        }



    }
}
